package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceMonthlyAddDTO;
import org.jsola.hr.dto.SubsidyProgAddDTO;
import org.jsola.hr.dto.SubsidyProgUpdateDTO;
import org.jsola.hr.query.SubsidyProgQuery;
import org.jsola.hr.service.ISubsidyProgService;
import org.jsola.hr.vo.SubsidyProgListVO;
import org.jsola.hr.vo.SubsidyProgVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrSubsidyProgController")
@RequestMapping(HrConstants.API_PATH + "/v1/subsidyProgs")
public class SubsidyProgController implements ISubsidyProgControllerAPI {

    @Autowired
    private ISubsidyProgService subsidyProgService;

    @Override
    @PostMapping
    public Result<SubsidyProgVO> save(@Valid @RequestBody SubsidyProgAddDTO subsidyProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工津贴方案权限校验

        return Result.success(
                subsidyProgService.save(subsidyProgAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SubsidyProgUpdateDTO subsidyProgUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工津贴方案权限校验

        int count = subsidyProgService.update(subsidyProgUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{subsidyProgId}")
    public Result<Integer> delete(@PathVariable String subsidyProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工津贴方案权限校验

        int count = subsidyProgService.deleteByIds(tokenUser, subsidyProgId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] subsidyProgIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工津贴方案权限校验

        int count = subsidyProgService.deleteByIds(tokenUser, subsidyProgIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{subsidyProgId}")
    public Result<SubsidyProgListVO> findById(@PathVariable String subsidyProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工津贴方案权限校验

        return Result.success(
            subsidyProgService.selectById(subsidyProgId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SubsidyProgListVO>> listAll(@Valid SubsidyProgQuery subsidyProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工津贴方案权限校验

        return Result.success(
            subsidyProgService.select(subsidyProgQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SubsidyProgListVO>> page(@Valid SubsidyProgQuery subsidyProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            subsidyProgService.selectPage(subsidyProgQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/addSubsidyProg")
    public Result<SubsidyProgVO> addSubsidyProg(@Valid @RequestBody SubsidyProgAddDTO subsidyProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工津贴方案权限校验

        return Result.success(
                subsidyProgService.addSubsidyProg(subsidyProgAddDTO, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/updateSubsidyProg")
    public Result<SubsidyProgVO> updateSubsidyProg(@Valid @RequestBody SubsidyProgUpdateDTO subsidyProgUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工津贴方案权限校验

        SubsidyProgVO subsidyProgVO = subsidyProgService.updateSubsidyProg(subsidyProgUpdateDTO, tokenUser);
        return Result.success(subsidyProgVO);
    }

    @Override
    @PostMapping(value = "empSubsidyCount")
    public Result<Boolean> empSubsidyCount(@Valid @RequestBody AttendanceMonthlyAddDTO attendanceMonthlyAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤扣款设置权限校验

        return Result.success(
                subsidyProgService.empSubsidyCount(tokenUser, attendanceMonthlyAddDTO)
        );
    }


}

